Hello and welcome back to our costs.
So in the last session I introduced multi-dimensional and umpire race and the most common cases to remember
a case where we have rows and columns.
Some of this session we learn how to index that slides two dimensional arrays and also how to change
singer or multiple elements in a two dimensional array.
So about first of all let's s always important umpire.
And then we create an umpire Ray from 1 to twelfth including and then we reshape those 12 elements to
three rows and four columns.
Now you could ask yourself why the elements are arranged in this manner.
So we have kind of a row vice order.
So we have twelve elements and reshape automatically orders them the elements row so starting in the
first row one two three four.
Then the second row of five six seven eight.
And theoretically could also be the other way around.
So starting column biased so one two three four five six and the others.
And that this is the default setting and an umpire.
But of course we can't change that soon.
They reshape method.
There is some part of me to court order and it's the default setting and see I don't want to go into
details what C means but we can change here the order to F and when we rerun here ourselves then we
get a different order.
So now we have one two three four five six and so on.
So this is flexible here.
All right so let's change it to order C and run the sides again.
Now again we f our let's say normal order and indexing multi-dimensional or two dimensional arrays.
That's actually no difference to one dimensional arrays.
So we have also zero based indexing and if we want to slice the first element of the array.
So the first element here is the first row.
We can do this by having the 0 in the spare brackets
and now we get to the first row of our array and we can also get the second row at an exposition one
and the third row at to next position to so your f in and 0 in exposition 1 and in exposition 2 and
we can also slice of the last show which is actually the third row.
This gives us the same result.
And what if now we want to slice them in the second row.
The second element so let's say here.
So we want to have in the second row The second element.
The sixth and we can do this by having two spat brackets.
So first of all let's cross the second one year out.
So first of all we are slicing the second row.
Yeah.
And then from the second row we are getting the second element in exposition 1
and extra.
We do not need to spell brackets you can put this into once bad bracket separated by a comma.
So the first position gets us in the row we want to slice into the second position gets us in the column
so spat bracket one come out.
One should give us the same as loud as them here above buffers two step records.
And that's correct.
So now we can also index the element in the third row and the last column column.
So let's see what it is on the third row in the last column it's twelve let's check this out and we
get twelve and out if you want to select a column so all rows and all elements in a column then we can
say okay I want f all rows I have a double colon and then I want to have the first column.
Now this gives us the complete first column 1 5 and 9
So all arrows in the first column and of course we can also select for all rows in the second column
to 610
and also all rows in the last column five eight twelve.
Now we can also do more complicated things like getting the elements in the first two rows and column
2 and 3.
So in the first two rows the elements 2 and 3 so 2 3 6 7
Yeah.
All right so let's go back to our array and what we can also do we can switch or change the x's or transpose
the Xs so to say that yeah instead of having a year one two three four as the first row we want to have
it as a first column and instead of having here in the first column 1 5 9 we want to have it as the
first row and we can do this by the attribute t so T capital letter
and then we see a we just transposed them the our the array we switched the axis and that's also the
method transpose.
So we have at the brackets you can see it's a method not an attribute.
And this gives us the same result.
All right so now let's assume let's go back to our India rate a let's assume you want to divide the
last column by 4 and actually we want to change the last column.
So let's first of all divide on elements by force so we select all rows.
And the last column and we will divide each element by four and then we have to assign the new values
to the last column.
So we have to say
so please for the last column please calculate new values
and now if you check a day you can see them on the last column.
We have divided all elements by far.
Okay so let's uh rearrange our array a well we know it already and then we can take the sum of all elements.
So that's uh the method that sum which sums of all elements in the matrix.
So it's seventy eight in what we can also do we can take the sum of each column so we can.
So we can take the sum of column 1 it's the 15 of column two it's 18 and so on and therefore we have
to define the axis.
So the axis is the argument or barometer of the sum method an axis equals zero means that we take the
sum of each column or to say along the rows so we sum up along the rows let's see what we get so we
get 15 or 15 a seal of the some of the first column we get 18.
It's the sum of the second column 21 is the sum of the third column and 24 is the sum of the fourth
column and we can also take the sum of each row.
So this means you take the sum along the columns.
So get we get 10 26 and 42.
So the sum of the first row gives us a 10.
Then the sum of the second draw gives us 26 and the sum of the third row gives us 42.
That's also the method come some and come some early returns.
The cumulative sum of all elements.
So let's see what we get.
It's a cumulative means we are starting from the first element.
So 1 then we are adding the second gives us three.
Then we are adding the third element gives us six.
Adding the fourth element gives us 10 adding the fifth element gives 15 and so on and we can also take
the cumulative sum for each column
from our let's secure the first column.
So we start with 1 then we add 5 to 6 then we add 9 to 15 and also the second column.
We have 2 adding six men we have eight adding 10 then we have 18 and we can also do the same cumulative
sum for each row.
All right.
So let's go back to our initial array a and that's also the method thought process.
So it takes the product over all elements.
That's a bit a large number here actually and we can also take the product over all elements in each
column so x equals zero.
I guess that's the product of the column.
So here we have 1 times five times line.
Give us forty five and so on so two times six is twelve times ten is under 20.
And yeah of course we can also take the product over all elements in each row by having the parameter
X equals one.
Now you can see it's the product here.
Over each row.
So one times two times three times four gets 24.
Yeah.
And so on.
All right.
So this is it for the session and in the next session we learn about boolean indexing.
So I hope to see you there by.